package com.gao.q_readwritelock.b;

import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * @author gao
 * @date 2023/03/14 17:52:49
 */
public class App {
    public static void main(String[] args) {
        test3();
    }

    private static void test() {
        ReadWriteLock rwl = new ReentrantReadWriteLock();
        // 在没有加 读/写 锁的情况下，直接释放 读/写 锁，会抛出异常
        rwl.readLock().unlock();
        // rwl.writeLock().unlock();
    }

    private static void test2() {
        // 当前线程，如果先获取写锁，后获取读锁，是不会阻塞的
        ReadWriteLock rwl = new ReentrantReadWriteLock();
        rwl.writeLock().lock();
        System.out.println("获取写锁");
        rwl.readLock().lock();
        System.out.println("获取读锁");
    }

    private static void test3() {
        // 当前线程，如果先获取读锁，后获取写锁，是会阻塞的
        ReadWriteLock rwl = new ReentrantReadWriteLock();
        rwl.readLock().lock();
        System.out.println("获取读锁");
        rwl.writeLock().lock();
        System.out.println("获取写锁");
    }
}
